package com.lt.suangua.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.lt.base.BaseAdapter;
import com.lt.suangua.R;
import com.lt.suangua.app.TitleBarFragment;
import com.lt.suangua.http.api.HomeApi;
import com.lt.suangua.ui.activity.BrowserActivity;
import com.lt.suangua.ui.activity.HomeActivity;
import com.lt.suangua.ui.adapter.ArticleAdapter;
import com.lt.suangua.ui.adapter.TabAdapter;
import com.lt.suangua.widget.XCollapsingToolbarLayout;

import java.io.Serializable;
import java.util.List;

public class ArticleFragment extends TitleBarFragment<HomeActivity> {
    ArticleAdapter articleAdapter;
    RecyclerView rv_article;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_article;
    }

    @Override
    protected void initView() {
        rv_article = findViewById(R.id.rv_article);

    }

    @Override
    protected void initData() {
        Bundle arguments = getArguments();
        HomeApi.ArticleArea articleArea = (HomeApi.ArticleArea) arguments.getSerializable("articleArea");
        String cate = arguments.getString("cate");
        if (articleArea != null) {
            List<HomeApi.ArticleList> articlelist = articleArea.articlelist;
            if (articlelist != null && articlelist.size() > 0) {
                for (int i = 0; i < articlelist.size(); i++) {
                    if (cate.equals(articlelist.get(i).cate)) {
                        articleAdapter = new ArticleAdapter(getActivity());
                        articleAdapter.setData(articlelist.get(i).list);
                        articleAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                                Log.d("clickUrl",articleAdapter.getItem(position).url);
                                BrowserActivity.start(getActivity(),articleAdapter.getItem(position).url,articleAdapter.getItem(position).title);
                            }
                        });
                        rv_article.setAdapter(articleAdapter);

                        break;
                    }
                }
            }
        }
    }
}
